package com.kkb.retailer.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.retailer.center.api.GoodsInfoService;
import com.kkb.retailer.center.common.constant.ConstantKey;
import com.kkb.retailer.center.common.pojo.*;
import com.kkb.retailer.center.server.mappers.GoodsAttachMapper;
import com.kkb.retailer.center.server.mappers.GoodsInfoMapper;
import com.kkb.retailer.center.server.mappers.ProductInfoMapper;
import com.kkb.retailer.center.server.mappers.ProductIntroduceMapper;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import tk.mybatis.mapper.entity.Example;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;


@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductIntroduceMapper productIntroduceMapper;


    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Autowired
    private GoodsAttachMapper goodsAttachMapper;

    @Autowired
    private TemplateEngine templateEngine;





    @Override
    public PageInfo<GoodsInfo> queryAllByCondition(GoodsQueryCondition condition) {
        //设置分页
        PageHelper.startPage(condition.getPage()+1,condition.getSize());
        //设置组合查询条件
        Example example = new Example(GoodsInfo.class);
        if(condition.getId()!=null){
            example.createCriteria().andEqualTo("id",condition.getId());
        }

        if(StringUtils.isNotEmpty(condition.getGoodsName())){
            example.createCriteria().andEqualTo("goodsName",condition.getGoodsName());

        }

        if(condition.getShopId()>0){
            example.createCriteria().andEqualTo("shopId",condition.getShopId());

        }

        if(condition.getStatus()!=null){
            example.createCriteria().andEqualTo("status",condition.getStatus());

        }

        List<GoodsInfo> goodInfos = goodsInfoMapper.selectByExample(example);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodInfos);
        return pageInfo;
    }

    @Override
    @Transactional
    public int addProductInfo(ProductInfo productInfo, String extrattribute, String introduce) {

        //添加产品

        productInfoMapper.insertUseGeneratedKeys(productInfo);
        //获取最新的产品ID
        Long productId = productInfo.getId();

        List<GoodsInfo> goodInfoList = productInfo.getGoodsList();

        int result = 0;

        for (GoodsInfo goodsInfo:goodInfoList){
            //设置当前商品对应的产品ID
            goodsInfo.setProductId(productId);
            goodsInfo.setShopId(productInfo.getShopId());
            goodsInfo.setStatus((byte)1);
            //插入商品对象
            goodsInfoMapper.insertSelective(goodsInfo);
            result++;
        }

        ProductIntroduce productIntroduce = new ProductIntroduce();
        productIntroduce.setProductId(productId);

        ProductIntroduceWithBLOBs productIntroduceWithBLOBs =
                new ProductIntroduceWithBLOBs();
        productIntroduceWithBLOBs.setExtrattribute(extrattribute);
        productIntroduceWithBLOBs.setIntroduce(introduce);

        productIntroduceMapper.insertSelective(productIntroduce);


        return result;
    }




    @Override
    public int deleteImgByGoodsId(Long goodsId) {
        Example example = new Example(GoodsAttach.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        return goodsAttachMapper.deleteByExample(example);
    }

    @Override
    public void relatedImg(GoodsAttach goodsAttach) {
        goodsAttachMapper.insertSelective(goodsAttach);
    }

    @Override
    public List<GoodsAttach> getImgs(Long goodsId) {
        Example example = new Example(GoodsAttach.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);

        return goodsAttachMapper.selectByExample(example);
    }

    @Override
    public int updateGoods(GoodsInfo goodsInfo) {
        int count = goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);

        //更新商品信息，生产者发送商品消息
        rocketMQTemplate.convertAndSend(ConstantKey.TOPIC_NAME,goodsInfo);
        return count;
    }

    @Override
    public String staticPageBuild(GoodsInfo goodsInfo) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("/Users/hadoop/Desktop/hello.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

        Context context = new Context();
        context.setVariable("goodsname",goodsInfo.getGoodsName());
        context.setVariable("costprice",goodsInfo.getCostPrice());
        templateEngine.process("hello",context,fileWriter);
        return "ok";
    }
}
